package cn.hanyi;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.network.jms.JmsConnector;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;


@SpringBootApplication(scanBasePackages = {"cn.**"})
@MapperScan("cn.repository")
public class ActivemqApplication {

	@Autowired
	ConnectionFactory connectionFactory;

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(ActivemqApplication.class, args);
			System.out.println("▁▂▃▄▅▆▇█    Spring Boot    █▇▆▅▄▃▂▁");

	}

	@Value("${spring.activemq.broker-url}")
	private String url;
	@Value("${spring.activemq.user}")
	private String user;
	@Value("${spring.activemq.password}")
	private String password;

	@Bean
	public ConnectionFactory connectionFactory(@Value("${spring.activemq.broker-url}") String url,@Value("${spring.activemq.user}")String user,
	    @Value("${spring.activemq.password}") String password){
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		connectionFactory.setBrokerURL(url);
		connectionFactory.setUserName(user);
		connectionFactory.setPassword(password);
		return connectionFactory;
	}

	@Bean
	public JmsTemplate jmsTemplate(){
		return new JmsTemplate(connectionFactory);
	}
	@Bean
	public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory() {

		DefaultJmsListenerContainerFactory factory =
				new DefaultJmsListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory);
		//设置连接数
		factory.setConcurrency("3-10");
		//重连间隔时间
		factory.setRecoveryInterval(1000L);
		return factory;

	}
}